package com.mldong.modules.sys.controller;

import com.mldong.auth.Perm;
import com.mldong.base.CommonPage;
import com.mldong.base.CommonResult;
import com.mldong.base.IdParam;
import com.mldong.base.IdsParam;
import com.mldong.holder.LoginUserHolder;
import com.mldong.modules.sys.dto.UserPageParam;
import com.mldong.modules.sys.dto.UserParam;
import com.mldong.modules.sys.service.UserService;
import com.mldong.modules.sys.vo.UserVO;
import com.mldong.validation.Groups;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
* <p>
    * 用户 前端控制器
    * </p>
*
* @author mldong
* @since 2022-10-19
*/
@RestController
@Api(tags = "用户管理")
@RequiredArgsConstructor
public class UserController {
    private final UserService userService;
    /**
     * 添加用户
     * @param param
     * @return
     */
    @PostMapping("/sys/user/save")
    @ApiOperation(value = "添加用户")
    @Perm
    public CommonResult<?> save(@RequestBody @Validated({Groups.Save.class}) UserParam param) {
        userService.save(param);
        return CommonResult.ok();
    }
    /**
     * 删除用户
     * @param param
     * @return
     */
    @PostMapping("/sys/user/remove")
    @ApiOperation(value = "删除用户")
    @Perm
    public CommonResult<?> remove(@RequestBody IdsParam param) {
        userService.removeBatchByIds(param.getIds());
        return CommonResult.ok();
    }
    /**
     * 修改用户
     * @param param
     * @return
     */
    @PostMapping("/sys/user/update")
    @ApiOperation(value = "修改用户")
    @Perm
    public CommonResult<?> update(@RequestBody @Validated({Groups.Update.class}) UserParam param) {
        userService.update(param);
        return CommonResult.ok();
    }
    /**
     * 查询单个用户
     * @param param
     * @return
     */
    @PostMapping("/sys/user/detail")
    @ApiOperation(value = "查询单个用户")
    @Perm
    public CommonResult<UserVO> getById(@RequestBody @Validated IdParam param) {
        UserVO user = userService.findById(param.getId());
        return CommonResult.data(user);
    }
    /**
     *分页查询用户列表
     * @param param
     * @return
     */
    @PostMapping("/sys/user/page")
    @ApiOperation(value = "分页查询用户列表")
    @Perm
    public CommonResult<CommonPage<UserVO>> page(@RequestBody UserPageParam param) {
        return CommonResult.data(userService.page(param));
    }
    /**
     * 重置密码
     * @param param
     * @return
     */
    @PostMapping("/sys/user/resetPassword")
    @ApiOperation(value = "重置密码")
    @Perm
    public CommonResult<?> resetPassword(@RequestBody @Validated IdsParam param) {
        userService.resetPassword(param.getIds());
        return CommonResult.ok();
    }
    /**
     * 个人-修改头像
     * @param param
     * @return
     */
    @PostMapping("/sys/user/updateAvatar")
    @ApiOperation(value = "个人-修改头像")
    public CommonResult<?> updateAvatar(@RequestBody @Validated({UserParam.ValidAvatar.class}) UserParam param) {
        userService.updateAvatar(param);
        return CommonResult.ok();
    }
    /**
     * 个人-修改用户信息
     * @param param
     * @return
     */
    @PostMapping("/sys/user/updateInfo")
    @ApiOperation(value = "个人-修改用户信息")
    public CommonResult<?> updateInfo(@RequestBody @Validated({UserParam.ValidInfo.class}) UserParam param) {
        userService.updateInfo(param);
        return CommonResult.ok();
    }
    /**
     * 个人-用户信息
     * @return
     */
    @PostMapping("/sys/user/info")
    @ApiOperation(value = "个人-用户信息")
    public CommonResult<UserVO> info() {
        Long userId = LoginUserHolder.getUserId();
        UserVO user = userService.findById(userId);
        user.setPassword(null);
        user.setSalt(null);
        return CommonResult.data(user);
    }
    /**
     * 个人-权限标识
     * @return
     */
    @PostMapping("/sys/user/permCode")
    @ApiOperation(value = "个人-权限标识")
    public CommonResult<List<String>> permCode() {
        List<String> permCodeList = LoginUserHolder.get().getPerms();
        return CommonResult.data(permCodeList);
    }
}
